#include "stdc.h"
#include "hash.h"

void makeHash(char* src, char* des, unsigned int len) {
	*(uint32_t*)des = BL32(ELFHash(src, len));
	*((uint32_t*)des + 1) = BL32(SDBMHash(src, len));
	*((uint32_t*)des + 2) = BL32(DJBHash(src, len));
}

uint32_t RSHash(char* str, unsigned int len) {
   uint32_t b    = 378551;
   uint32_t a    = 63689;
   uint32_t hash = 0;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash = hash * a + (*str);
      a    = a * b;
   }
   return hash;
}
/* End Of RS Hash Function */


uint32_t JSHash(char* str, unsigned int len) {
   uint32_t hash = 1315423911;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash ^= ((hash << 5) + (*str) + (hash >> 2));
   }
   return hash;
}
/* End Of JS Hash Function */


uint32_t PJWHash(char* str, unsigned int len) {
   const uint32_t BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
   const uint32_t ThreeQuarters     = (unsigned int)((BitsInUnsignedInt  * 3) / 4);
   const uint32_t OneEighth         = (unsigned int)(BitsInUnsignedInt / 8);
   const uint32_t HighBits          = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
   uint32_t hash              = 0;
   uint32_t test              = 0;
   unsigned int i                 = 0;

   for(i = 0; i < len; str++, i++) {
      hash = (hash << OneEighth) + (*str);

      if((test = hash & HighBits)  != 0) {
         hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
      }
   }
   return hash;
}
/* End Of  P. J. Weinberger Hash Function */


uint32_t ELFHash(char* str, unsigned int len) {
   uint32_t hash = 0;
   uint32_t x    = 0;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash = (hash << 4) + (*str);
      if((x = hash & 0xF0000000L) != 0) {
         hash ^= (x >> 24);
      }
      hash &= ~x;
   }
   return hash;
}
/* End Of ELF Hash Function */


uint32_t BKDRHash(char* str, unsigned int len) {
   uint32_t seed = 131; /* 31 131 1313 13131 131313 etc.. */
   uint32_t hash = 0;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash = (hash * seed) + (*str);
   }
   return hash;
}
/* End Of BKDR Hash Function */


uint32_t SDBMHash(char* str, unsigned int len) {
   uint32_t hash = 0;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash = (*str) + (hash << 6) + (hash << 16) - hash;
   }
   return hash;
}
/* End Of SDBM Hash Function */


uint32_t DJBHash(char* str, unsigned int len) {
   uint32_t hash = 5381;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash = ((hash << 5) + hash) + (*str);
   }
   return hash;
}
/* End Of DJB Hash Function */


uint32_t DEKHash(char* str, unsigned int len) {
   uint32_t hash = len;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
   }
   return hash;
}
/* End Of DEK Hash Function */


uint32_t BPHash(char* str, unsigned int len) {
   uint32_t hash = 0;
   unsigned int i    = 0;
   for(i = 0; i < len; str++, i++) {
      hash = hash << 7 ^ (*str);
   }
   return hash;
}
/* End Of BP Hash Function */


uint32_t FNVHash(char* str, unsigned int len) {
   const uint32_t fnv_prime = 0x811C9DC5;
   uint32_t hash      = 0;
   unsigned int i         = 0;

   for(i = 0; i < len; str++, i++) {
      hash *= fnv_prime;
      hash ^= (*str);
   }
   return hash;
}
/* End Of FNV Hash Function */


uint32_t APHash(char* str, unsigned int len) {
   uint32_t hash = 0xAAAAAAAA;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++) {
      hash ^= ((i & 1) == 0) ? (  (hash <<  7) ^ (*str) * (hash >> 3)) :
                               (~((hash << 11) + ((*str) ^ (hash >> 5))));
   }
   return hash;
}
/* End Of AP Hash Function */
